﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Food
{
    public class IFoodBrand
    {
        void AddFoodBrand(string brandName, string brandDescription, Model.HealthRating healthRating, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            Model.FoodBrand brand = new Model.FoodBrand();
            brand.FoodBrandName = brandName;
            brand.FoodBrandDescription = brandDescription;
            brand.HealthRating = healthRating;
            context.FoodBrands.AddObject(brand);
            context.SaveChanges();
        }

        void AddFoodBrand(string brandName, string brandDescription, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            Model.FoodBrand brand = new Model.FoodBrand();
            brand.FoodBrandName = brandName;
            brand.FoodBrandDescription = brandDescription;
            context.FoodBrands.AddObject(brand);
            context.SaveChanges();
        }

        int GetBrandId(string BrandName, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            if (context.FoodBrands.Where(p => p.FoodBrandName.CompareTo(BrandName) == 0).Count() == 0)
            {

                return context.FoodBrands.Where(p => p.FoodBrandName.CompareTo(BrandName) == 0).First().FoodBrandID;
            }
            else
                return -1;
        }

        Model.FoodBrand GetFoodBrand(int brandId, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {

            if (context.FoodBrands.Where(p => p.FoodBrandID == brandId).Count() == 0)
            {
                return context.FoodBrands.Where(p => p.FoodBrandID == brandId).First();
            }
            else
                return null;


        }

        void UpdateFoodBrand(Model.FoodBrand brand, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            if (context.FoodBrands.Where(p => p.FoodBrandID == brand.FoodBrandID).Count() == 0)
            {
                Model.FoodBrand foodbrand = context.FoodBrands.Where(p => p.FoodBrandID == brand.FoodBrandID).First();
                context.DeleteObject(foodbrand);
                context.SaveChanges();
                context.FoodBrands.AddObject(foodbrand);
                context.SaveChanges();
            }
        }

        void RemoveBrand(int brandId, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            if (context.FoodBrands.Where(p => p.FoodBrandID == brandId).Count() == 0)
            {
                context.FoodBrands.DeleteObject(context.FoodBrands.Where(p => p.FoodBrandID == brandId).First());
                context.SaveChanges();
            }
        }

        void RemoveBrand(string brandName, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            if (context.FoodBrands.Where(p => p.FoodBrandName.CompareTo(brandName) == 0).Count() == 0) {
                Model.FoodBrand brand = context.FoodBrands.Where(p => p.FoodBrandName.CompareTo(brandName) == 0).First();
                context.DeleteObject(brandName);
                context.SaveChanges();
            }


        }

        void RemoveBrand(Model.FoodBrand brand, ref NutriLeaf.Web.Model.NutriLeafDBEntities context)
        {
            context.FoodBrands.DeleteObject(brand);
            context.SaveChanges();

        }
    }
}